#include "toccharmodel.h"
#include <QDebug>

TocCharModel::TocCharModel(QObject *parent) :
    QObject(parent)
{
    chAbilities.append("Run");
    chAbilities.append("Go");
    chAbilities.append("Wee");
}

// PROPERTY GETTERS
QString TocCharModel::playerName() const
{
    return plName;
}

QString TocCharModel::characterName() const
{
    return chName;
}

QString TocCharModel::occupation() const
{
    return chOcp;
}

QString TocCharModel::occupationBenefits() const
{
    return chOcpBnf;
}

QString TocCharModel::drive() const
{
    return chDrive;
}

QString TocCharModel::pillars() const
{
    return chPillars;
}

int TocCharModel::toHit() const
{
    return chToHit;
}

int TocCharModel::buildPoints() const
{
    return chBuildPoints;
}

int TocCharModel::skillPoints() const
{
    return chSkillPoints;
}

QVariantList TocCharModel::abilities() const
{
    return chAbilities;
}

QVariantList TocCharModel::occupAbilities() const
{
    return chOccupAbilities;
}

QVariantList TocCharModel::clues() const
{
    return chClues;
}

QVariantList TocCharModel::contacts() const
{
    return chContacts;
}

QVariantList TocCharModel::weapons() const
{
    return chWeapons;
}

QString TocCharModel::clipboard() const
{
    return chClipboard;
}

QString TocCharModel::dataVersion() const
{
    return chDataVersion;
}

QString TocCharModel::dataLocale() const
{
    return chDataLocale;
}

// PROPERTY SETTERS
void TocCharModel::setPlayerName(QString name)
{
    qDebug() << "Set Player Name: " << name;
    plName = name;
    emit playerNameChanged(plName);
}

void TocCharModel::setCharacterName(QString name)
{
    qDebug() << "Set Character Name: " << name;
    chName = name;
    emit characterNameChanged(chName);
}

void TocCharModel::setOccupation(QString value)
{
    qDebug() << "Set Occupation: " << value;
    chOcp = value;
    emit occupationChanged(chOcp);
}

void TocCharModel::setOccupationBenefits(QString value)
{
    qDebug() << "Set Occupation Benefits: " << value;
    chOcpBnf = value;
    emit occupationBenefitsChanged(chOcpBnf);
}

void TocCharModel::setDrive(QString value)
{
    qDebug() << "Set Drive: " << value;
    chDrive = value;
    emit driveChanged(chDrive);
}

void TocCharModel::setPillars(QString value)
{
    chPillars = value;
    emit pillarsChanged(chPillars);
}

void TocCharModel::setToHit(int value)
{
    qDebug() << "Set ToHit: " << value;
    chToHit = value;
    emit toHitChanged(chToHit);
}

void TocCharModel::setBuildPoints(int value)
{
    qDebug() << "Set Build Points: " << value;
    chBuildPoints = value;
    emit buildPointsChanged(chBuildPoints);
}

void TocCharModel::setSkillPoints(int value)
{
    qDebug() << "Set Skill Points: " << value;
    chSkillPoints = value;
    emit skillPointsChanged(chSkillPoints);
}

void TocCharModel::setAbilities(QVariantList list)
{
    chAbilities = list;
}

void TocCharModel::setOccupAbilities(QVariantList list)
{
    chOccupAbilities = list;
}

void TocCharModel::setClues(QVariantList list)
{
    chClues = list;
}

void TocCharModel::setContacts(QVariantList list)
{
    chContacts = list;
}

void TocCharModel::setWeapons(QVariantList list)
{
    chWeapons = list;
}

void TocCharModel::setClipboard(QString value)
{
    chClipboard = value;
}

void TocCharModel::setDataVersion(QString value)
{
    chDataVersion = value;
}

void TocCharModel::setDataLocale(QString value)
{
    chDataLocale = value;
}

int TocCharModel::calculateSpentBp() const
{
    int tot = 0;
    foreach( QVariant ab, chAbilities )
    {
        QVariantMap abMap = ab.toMap();
        QString categ = abMap.value("categ").toString();
        int rank = abMap.value("rank").toInt();
        bool prof = abMap.value("prof", false).toBool();
        if ( categ != tr("General") )
            continue;
        double cost = rank / ( prof ? 2 : 1 );
        tot += (int)(cost+0.5);
    }

    tot -= 6; // freebies
    return tot;
}

int TocCharModel::calculateSpentSp() const
{
    int tot = 0;
    foreach( QVariant ab, chAbilities )
    {
        QVariantMap abMap = ab.toMap();

        QString categ = abMap.value("categ").toString();
        int rank = abMap.value("rank").toInt();
        bool prof = abMap.value("prof", false).toBool();
        if ( categ == tr("General") )
            continue;
        double cost = rank / ( prof ? 2 : 1 );
        tot += (int)(cost+0.5);
    }

    // TODO: starting reputation should not be counted

    return tot;
}
